package com.ruoyi.entity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.entity.domain.Option;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.entity.domain.MsgLog;
import com.ruoyi.entity.service.IMsgLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息日志Controller
 *
 * @author Delamer
 * @date 2020-12-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/entity/log")
public class MsgLogController extends BaseController {

    private final IMsgLogService msgLogService;

    /**
     * 查询消息日志列表
     */
    @PreAuthorize("@ss.hasPermi('entity:log:list')")
    @GetMapping("/list")
    public TableDataInfo list(MsgLog msgLog) {
        startPage();
        LambdaQueryWrapper<MsgLog> lqw = new LambdaQueryWrapper<MsgLog>();
        if (msgLog.getGroupId() != null) {
            lqw.eq(MsgLog::getGroupId, msgLog.getGroupId());
        }
        if (msgLog.getUserId() != null) {
            lqw.eq(MsgLog::getUserId, msgLog.getUserId());
        }
        if (StringUtils.isNotBlank(msgLog.getUserName())) {
            lqw.like(MsgLog::getUserName, msgLog.getUserName());
        }
        if (msgLog.getMsgId() != null) {
            lqw.eq(MsgLog::getMsgId, msgLog.getMsgId());
        }
        if (StringUtils.isNotBlank(msgLog.getMsgBody())) {
            lqw.eq(MsgLog::getMsgBody, msgLog.getMsgBody());
        }
        if (StringUtils.isNotBlank(msgLog.getMsgContent())) {
            lqw.eq(MsgLog::getMsgContent, msgLog.getMsgContent());
        }
        if (StringUtils.isNotBlank(msgLog.getMsgType())) {
            lqw.eq(MsgLog::getMsgType, msgLog.getMsgType());
        }
        if (StringUtils.isNotBlank(msgLog.getSubType())) {
            lqw.eq(MsgLog::getSubType, msgLog.getSubType());
        }
        if (StringUtils.isNotBlank(msgLog.getPostType())) {
            lqw.eq(MsgLog::getPostType, msgLog.getPostType());
        }
        lqw.orderByDesc(MsgLog::getMsgId);
        List<MsgLog> list = msgLogService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出消息日志列表
     */
    @PreAuthorize("@ss.hasPermi('entity:log:export')")
    @Log(title = "消息日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MsgLog msgLog) {
        LambdaQueryWrapper<MsgLog> lqw = new LambdaQueryWrapper<MsgLog>(msgLog);
        List<MsgLog> list = msgLogService.list(lqw);
        ExcelUtil<MsgLog> util = new ExcelUtil<MsgLog>(MsgLog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 获取消息日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('entity:log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(msgLogService.getById(id));
    }

    /**
     * 新增消息日志
     */
    @PreAuthorize("@ss.hasPermi('entity:log:add')")
    @Log(title = "消息日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MsgLog msgLog) {
        return toAjax(msgLogService.save(msgLog) ? 1 : 0);
    }

    /**
     * 修改消息日志
     */
    @PreAuthorize("@ss.hasPermi('entity:log:edit')")
    @Log(title = "消息日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MsgLog msgLog) {
        return toAjax(msgLogService.updateById(msgLog) ? 1 : 0);
    }

    /**
     * 删除消息日志
     */
    @PreAuthorize("@ss.hasPermi('entity:log:remove')")
    @Log(title = "消息日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(msgLogService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
